package com.qf.direct;

import com.qf.util.RabbitMQUtil;
import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import org.junit.Test;

/**
 * @author zjw
 * @description
 * @date 2021/11/8 17:20
 */
public class Publisher {

    @Test
    public void publish() throws Exception {
        //1. 获取连接对象
        Connection connection = RabbitMQUtil.getConnection();

        //2. 构建Channel
        Channel channel = connection.openChannel().get();

        //3. 构建交换机
        channel.exchangeDeclare("routing_exchange", BuiltinExchangeType.DIRECT);
        channel.queueBind("routing_queue1","routing_exchange","ERROR");
        channel.queueBind("routing_queue1","routing_exchange","INFO");
        channel.queueBind("routing_queue1","routing_exchange","DEBUG");
        channel.queueBind("routing_queue2","routing_exchange","ERROR");

        //4. 发送消息
        channel.basicPublish("routing_exchange","ERROR",null,"errorMsg".getBytes());
        channel.basicPublish("routing_exchange","INFO",null,"infoMsg".getBytes());
        channel.basicPublish("routing_exchange","WARNING",null,"warningMsg".getBytes());

        //5. 释放资源
        channel.close();
        connection.close();
    }

}
